package com.lzy.cloud.member.facade;

import com.lzy.cloud.member.dtos.MemberCouponDto;
import com.lzy.cloud.member.remote.CouponApi;
import com.lzy.cloud.member.remote.dtos.BaseCouponDto;
import com.lzy.cloud.member.service.MemberCouponService;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WIN7 on 2018/5/30.
 */
@Component
public class MemberCouponFacade {

    @Autowired
    MemberCouponService memberCouponService;
    @Autowired
    CouponApi couponApi;

    /**
     * 根据会员查询卡券
     * @param memberId
     * @return
     */
    public List<BaseCouponDto> findCarRentByMemberId(Long memberId){
        List<Long> ids = new ArrayList<>();
        MemberCouponDto dto = new MemberCouponDto();
        dto.setMemberId(memberId);
        List<MemberCouponDto> memberCouponDtoList = memberCouponService.queryList(dto);
        for (int i = 0; i < memberCouponDtoList.size(); i++) {
            ids.add(memberCouponDtoList.get(i).getId());
        }
        Result result = couponApi.findCarRentByIds(ids);

        List<BaseCouponDto> list = DozerUtils.maps((List)result.getResult(), BaseCouponDto.class);
        return list;
    }

    /**
     * 根据卡券id获取会员卡券关系
     * @param couponId
     * @return
     */
    public MemberCouponDto findByCouponId(Long couponId){
        MemberCouponDto memberCouponDto = new MemberCouponDto();
        memberCouponDto.setCouponId(couponId);
        List<MemberCouponDto> memberCouponDtoList = memberCouponService.queryList(memberCouponDto);
        if(memberCouponDtoList != null && memberCouponDtoList.size() > 0){
            return memberCouponDtoList.get(0);
        }
        return null;
    }
}
